package com.kye.express.business.vipshop.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.vipshop.model.VipExchangeMoneyModel;
import com.kye.express.databinding.ItemVipMoneyTagBinding;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 11/02/2018
 * @updated : 11/02/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class VipExchangeViewBinder extends ItemViewBinder<VipExchangeMoneyModel, VipExchangeViewBinder.ViewHolder> {

    private OnItemListener onItemListener;

    public VipExchangeViewBinder(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_vip_money_tag, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull VipExchangeMoneyModel item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private VipExchangeMoneyModel item;
        private ItemVipMoneyTagBinding binding;
        private View itemView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.tagName)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            List<VipExchangeMoneyModel> items = (List<VipExchangeMoneyModel>) getAdapter().getItems();
                            Observable.just(item)
                                    .doOnNext(new Consumer<VipExchangeMoneyModel>() {
                                        @Override
                                        public void accept(VipExchangeMoneyModel moneyModel) throws Exception {
                                            moneyModel.setSelected(true);
                                        }
                                    })
                                    .flatMap(new Function<VipExchangeMoneyModel, ObservableSource<VipExchangeMoneyModel>>() {
                                        @Override
                                        public ObservableSource<VipExchangeMoneyModel> apply(VipExchangeMoneyModel moneyModel) throws Exception {
                                            return Observable.fromIterable(items);
                                        }
                                    })
                                    .map(new Function<VipExchangeMoneyModel, VipExchangeMoneyModel>() {
                                        @Override
                                        public VipExchangeMoneyModel apply(VipExchangeMoneyModel moneyModel) throws Exception {
                                            moneyModel.setSelected(moneyModel.equals(item));
                                            return moneyModel;
                                        }
                                    })
                                    .toList()
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Consumer<List<VipExchangeMoneyModel>>() {
                                        @Override
                                        public void accept(List<VipExchangeMoneyModel> vipExchangeMoneyModels) throws Exception {
                                            getAdapter().notifyDataSetChanged();
                                            if (onItemListener != null) {
                                                onItemListener.OnItemClicked(item);
                                            }
                                        }
                                    });
                        }
                    });
        }

        public void bindData(VipExchangeMoneyModel item) {
            this.item = item;
            int white = itemView.getResources().getColor(R.color.white);
            int pink = itemView.getResources().getColor(R.color.purple_C2539C);
            binding.tagName.setBackgroundResource(item.isSelected() ? R.drawable.background_round_corner_pink : R.drawable.background_common_white_frame_pink);
            binding.tagName.setTextColor(item.isSelected() ? white : pink);
            binding.tagName.setText(String.format("¥%d", item.getMoney()));
        }
    }

    public interface OnItemListener {
        void OnItemClicked(VipExchangeMoneyModel item);
    }
}
